package com.example.medicalrecord.AggregationModule.medicineOption.service.impl;

import com.example.medicalrecord.AggregationModule.medicine.mapper.MedicineMapper;
import com.example.medicalrecord.AggregationModule.medicine.pojo.dto.MedicineQuery;
import com.example.medicalrecord.AggregationModule.medicine.pojo.vo.MedicineVO;
import com.example.medicalrecord.AggregationModule.medicineOption.mapper.MedicineOptionMapper;
import com.example.medicalrecord.AggregationModule.medicineOption.pojo.dto.MedicineOptionParam;
import com.example.medicalrecord.AggregationModule.medicineOption.pojo.dto.MedicineOptionQuery;
import com.example.medicalrecord.AggregationModule.medicineOption.pojo.entity.MedicineOption;
import com.example.medicalrecord.AggregationModule.medicineOption.pojo.vo.MedicineOptionVO;
import com.example.medicalrecord.AggregationModule.medicineOption.service.MedicineOptionService;
import com.example.medicalrecord.base.exception.ServiceException;
import com.example.medicalrecord.base.response.PageData;
import com.example.medicalrecord.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class
MedicineOptionServiceImpl implements MedicineOptionService {
    @Autowired
    MedicineOptionMapper medicineOptionMapper;
    @Autowired
    MedicineMapper medicineMapper;

    @Override
    public PageData selectMedicineOption(MedicineOptionQuery medicineOptionQuery) {
        log.debug("查询药品项数据业务:medicineOptionQuery={}",medicineOptionQuery);
        List<MedicineOptionVO> medicineOptionVOList = medicineOptionMapper.selectMedicineOption(medicineOptionQuery);
        PageData pageData = new PageData();
        pageData.setTotal(medicineOptionMapper.selectMedicineOptionCount(medicineOptionQuery));
        pageData.setList(medicineOptionVOList);
        return pageData;
    }

    @Override
    public void saveMedicineOption(MedicineOptionParam medicineOptionParam) {
        log.debug("保存药品项数据业务:medicineOptionParam={}",medicineOptionParam);
        MedicineOption medicineOption = new MedicineOption();
        BeanUtils.copyProperties(medicineOptionParam,medicineOption);
        //        判断id是否为空,id为空代表新增数据,id不为空代表更新数据
        if (medicineOption.getId() == null){
            medicineOption.setCreateTime(new Date());
            medicineOptionMapper.insert(medicineOption);
        }else {
            medicineOption.setUpdateTime(new Date());
            medicineOptionMapper.update(medicineOption);
        }
    }

    @Override
    public void deleteMedicineOption(Long id) {
        log.debug("删除药品业务:id={}",id);
        medicineOptionMapper.delete(id);
    }

    @Override
    public List<MedicineOptionVO> selectMedicineOptionByCode(String code) {
        log.debug("根据药品类code查询查询药品项数据业务:code={}",code);
        MedicineQuery medicineQuery = new MedicineQuery();
        medicineQuery.setCode(code);
        List<MedicineVO> medicineVOList = medicineMapper.selectMedicine(medicineQuery);
        if (medicineVOList != null && medicineVOList.size()>0){
            Long medicineId = medicineVOList.get(0).getId();
            MedicineOptionQuery medicineOptionQuery = new MedicineOptionQuery();
            medicineOptionQuery.setMedicineId(medicineId);
            List<MedicineOptionVO> medicineOptionVOList = medicineOptionMapper.selectMedicineOption(medicineOptionQuery);
            return medicineOptionVOList;
        }else {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }


    }
}
